using System;
using System.Collections.Generic;
using UnityEngine;

public class WindowManager : MonoBehaviour
{
	public BaseWindow[] allWindows;

	public static WindowManager instance;

	private Dictionary<Type, BaseWindow> _windowRegistry = new Dictionary<Type, BaseWindow>();

	private List<BaseWindow> _shownWindows = new List<BaseWindow>();

	private void Awake()
	{
		instance = this;
		allWindows = GetComponentsInChildren<BaseWindow>(true);
		BaseWindow[] array = allWindows;
		foreach (BaseWindow baseWindow in array)
		{
			baseWindow.gameObject.SetActive(true);
			_windowRegistry.Add(baseWindow.GetType(), baseWindow);
		}
	}

	private void Start()
	{
		BaseWindow[] array = allWindows;
		foreach (BaseWindow baseWindow in array)
		{
			baseWindow.OnInit();
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public BaseWindow GetTopWindow()
	{
		if (_shownWindows == null || _shownWindows.Count == 0)
		{
			return null;
		}
		return _shownWindows[_shownWindows.Count - 1];
	}

	public void ShowWindow(Type windowClass, bool needHideOther)
	{
		ShowWindow(windowClass, new WindowShowParams(needHideOther, null));
	}

	public void ShowWindow(Type windowClass, WindowShowParams showParams = null)
	{
		if (!_windowRegistry.ContainsKey(windowClass))
		{
			return;
		}
		BaseWindow baseWindow = _windowRegistry[windowClass];
		if (showParams == null)
		{
			showParams = WindowShowParams.empty;
		}
		if (showParams.needHideOther)
		{
			for (int i = 0; i < _shownWindows.Count; i++)
			{
				BaseWindow baseWindow2 = _shownWindows[i];
				if (baseWindow2 != baseWindow)
				{
					baseWindow2.Hide();
					i--;
				}
			}
		}
		baseWindow.OnPreShow(showParams.userData);
		if (!baseWindow.isShow)
		{
			baseWindow.gameObject.SetActive(true);
			_shownWindows.Add(baseWindow);
			baseWindow.isShow = true;
		}
		baseWindow.isVisible = true;
		baseWindow.OnShow(showParams.userData);
	}

	public void HideWindow(BaseWindow window)
	{
		if (!(window == null) && window.isShow)
		{
			window.OnHide();
			window.gameObject.SetActive(false);
			window.isVisible = false;
			_shownWindows.Remove(window);
			window.isShow = false;
		}
	}

	public void HideWindow(Type windowClass)
	{
		if (_windowRegistry.ContainsKey(windowClass))
		{
			BaseWindow window = _windowRegistry[windowClass];
			HideWindow(window);
		}
	}

	public void HideAllWindow()
	{
		BaseWindow[] array = allWindows;
		foreach (BaseWindow window in array)
		{
			HideWindow(window);
		}
	}
}
